package org.wenhuayuan.project.common.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StringRes
import org.wenhuayuan.project.common.R

class EmpetyView  : LinearLayout {
    private var icon: TextView? = null
    private var title: TextView? = null
    private var button: Button? = null

//    @JvmOverloads
//    constructor(context: Context) : this(context, null)
//    constructor(context: Context, attributes: AttributeSet?) : this(context, attributes, 0)
    @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0):super(context, attrs, defStyleAttr){
        orientation = VERTICAL
        gravity = Gravity.CENTER
        LayoutInflater.from(context).inflate(R.layout.layout_empty_view, this, true)
        icon = findViewById<TextView>(R.id.empty_icon)
        title = findViewById<TextView>(R.id.empty_title)
        button = findViewById(R.id.empty_action)

    }

    fun setIcon(@StringRes iconRes: Int) {
        icon?.setText(iconRes)
    }
    fun setTitle(text: String) {
        title?.setText(text)
        title?.visibility = if (TextUtils.isDigitsOnly(text)) View.GONE else View.VISIBLE
    }

    fun setButton(text: String, listener: OnClickListener){
        button?.let { btn ->
            if (TextUtils.isEmpty(text)) {
                btn.visibility = View.GONE
            } else {
                btn.visibility = View.VISIBLE
                btn.setText(text)
                btn.setOnClickListener { listener }
            }
        }

    }


}